package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.StudentMapper;
import mianyang.zhangsan.com.pojo.Student;
import mianyang.zhangsan.com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 根据用户名查询学生信息
     * @param username
     * @return
     */
    @Override
    public Student queryInfoByUsername(String username) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return studentMapper.selectOne(wrapper);
    }

    @Override
    public Page<Student> queryStudentByPage(int page, int size, int clazzId) {
        QueryWrapper<Student> wrapper = null;
        if (clazzId != 0){
            wrapper = new QueryWrapper<>();
            wrapper.eq("clazz_id", clazzId);
        }
        return studentMapper.selectPage(new Page<>(page, size), wrapper);
    }

    @Override
    public String queryUserNameById(Integer studentId) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("id", studentId);
        Student student = studentMapper.selectOne(wrapper);
        if ("".equals(student.getUsername()) || student.getUsername() == null){
            return "not found";
        }
        return student.getUsername();
    }

    @Override
    public List<Student> queryInfoAll() {
        return studentMapper.selectList(null);
    }
}
